import datetime
import time
from google.appengine.ext import db

# Model for Pour Transactions
class Pour(db.Model):
    timestamp = db.DateTimeProperty(auto_now_add=True)
    kegdroid_name = db.StringProperty(required=False);
    kegdroid_android_id = db.StringProperty();
    drinker_gplus_id = db.StringProperty();
    beer_id = db.IntegerProperty();
    kegdroid_tap_number = db.IntegerProperty(required=False);
    pour_volume = db.FloatProperty();
    kegdroid_remaining_volume = db.FloatProperty();


    def to_dict(self):
        SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
        output = {}
        output["ID"] = self.key().id()
        for key, prop in self.properties().iteritems():
            value = getattr(self, key)
            if isinstance(value, SIMPLE_TYPES):
                output[key] = value
            elif value is None:
                output[key] = ""
            elif isinstance(value, datetime.date):
                # Convert date/datetime to ms-since-epoch ("new Date()").
                ms = time.mktime(value.utctimetuple())
                ms += getattr(value, 'microseconds', 0) / 1000
                output[key] = int(ms)
            elif isinstance(value, db.Model):
                output[key] = to_dict(value)
            else:
                raise ValueError('cannot encode ' + repr(prop))
        return output
        #return simplejson.dumps(output).replace("\/", "/")

